﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Work.Hardware;

namespace LightControl
{
    public partial class 光源 : Form
    {
        LightParam lp = null;
        LightSource sl = null;
        public 光源()
        {
            InitializeComponent();
            lp = new LightParam();
        }

        private void ConfirLightBtn_Click(object sender, EventArgs e)
        {
            int type = 1;
            if (int.TryParse(LightsType_Comb.Text, out type))
                lp.LightType = type;
        }

        private void ConnectBtn_Click(object sender, EventArgs e)
        {
            lp.Com = ComType_Comb.Text;
            try
            {
                sl = LightSourceFactory.GreatFactory(lp.Com, lp.LightType);
                ComStatuslabel.Text = string.Format( "已连接:{0}",lp.Com );
                ChControlGrBox.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("连接异常：" + ex.Message);
            }
        }

        private void DisConnectBtn_Click(object sender, EventArgs e)
        {
            if (sl != null)
            {
                sl.DisConnect();
                ComStatuslabel.Text = "未连接";
                ChControlGrBox.Enabled = false;
            }
        }

        private void ConfirChBtn_Click(object sender, EventArgs e)
        {
            byte ch = 1;
            if (byte.TryParse(Channel_Comb.Text, out ch))
                lp.Channel = ch;
            Chlabel.Text = string.Format("已选通道:{0}", ch.ToString());
        }

        private void SetBrightBtn_Click(object sender, EventArgs e)
        {
            byte inten = 0;
            if (byte.TryParse(Inten_Tx.Text, out inten))
                lp.Intensity = inten;
            if (sl != null)
                sl.SetIntensity(lp.Channel, lp.Intensity);
            IntensityLabel.Text = string.Format("光源亮度：{0}", lp.Intensity);
        }

        private void OpenBtn_Click(object sender, EventArgs e)
        {
            if (sl != null)
                sl.OpenSource(lp.Channel, lp.Intensity);
        }

        private void CloseBtn_Click(object sender, EventArgs e)
        {
            if (sl != null)
                sl.CloseSource(lp.Channel);
        }

        private void 光源_Load(object sender, EventArgs e)
        {
            string[] portlist = System.IO.Ports.SerialPort.GetPortNames();
            for (int i = 0; i < portlist.Length; i++)
            {
                this.ComType_Comb.Items.Add(portlist[i]);
            }
        }

    }
}
